package com.xiaomaoguai.shrio.chapter2.reaml;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;

/**
 * @Description :
 * @FileName: MyRealm1.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年5月11日 下午4:44:32
 * @Version:V1.00
 */
public class MyRealm1 implements Realm {

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = (String) token.getPrincipal();
		System.out.println(username);
		String password = new String((char[])token.getCredentials()); //得到密码
		System.out.println(password);
		if (!"zhang".equals(username)) {
			throw new UnknownAccountException(); // 如果用户名错误
		}
		if (!"123".equals(password)) {
			throw new IncorrectCredentialsException(); // 如果密码错误
		}
		System.out.println("here");
		return new SimpleAuthenticationInfo(username, password, getName());
	}

	@Override
	public String getName() {
		return "myrealm1";
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		return token instanceof UsernamePasswordToken; // 仅支持UsernamePasswordToken类型的Token
	}

}
